package com.yami.shop.api.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.UserFundDetail;
import com.yami.shop.bean.vo.UserFundDetailVo;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.UserFundDetailService;
import com.yami.shop.service.UserService;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户资金详情 前端控制器
 * </p>
 *
 * @author jcd
 * @since 2024-07-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/p/user/fundDetail")
public class UserFundDetailController {
    private final UserFundDetailService userFundDetailService;

    @Autowired
    private UserService userService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param fundDetail 用户地址管理
     * @return 分页数据
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<UserFundDetail>> getUserAddrPage(PageParam page, UserFundDetailVo fundDetail) {
        String userId = SecurityUtils.getUser().getUserId();

        LambdaQueryWrapper<UserFundDetail> userFundDetailLambdaQueryWrapper = new LambdaQueryWrapper<>(fundDetail);

        userFundDetailLambdaQueryWrapper.eq(UserFundDetail::getUid,userId);

        userFundDetailLambdaQueryWrapper.eq(StringUtils.isNotBlank(fundDetail.getCategory()),UserFundDetail::getCategory,fundDetail.getCategory());

        userFundDetailLambdaQueryWrapper.orderByDesc(UserFundDetail::getCreateTime);
        PageParam page1 = userFundDetailService.page(page,userFundDetailLambdaQueryWrapper);

        List<UserFundDetail> records = page1.getRecords();

        List<UserFundDetailVo> list1=new ArrayList<>();
        for (UserFundDetail detail:records){
            UserFundDetailVo vo=new UserFundDetailVo();
            BeanUtil.copyProperties(detail,vo);

            User byId = userService.getById(detail.getUid());
            if (byId != null){
                vo.setUserName(byId.getNickName());
            }
            list1.add(vo);
        }

        page1.setRecords(list1);
        return ServerResponseEntity.success(page1);
    }
}

